package com.douqu.game.configManager;

import com.bean.core.util.Utils;
import com.douqu.game.fileManager.ExcelUtils;
import com.douqu.game.MainFrame;
import com.douqu.game.MenuManager;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.IllegalFormatException;

/**
 * @author: miracle.
 * Description:configCreate
 * Date: 2018/8/8 0008 下午 2:56
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class ConfigCreate extends MenuManager {

    //      ******     服务器ID   活动服ID  数据库地址  数据库端口 用户名  密码    腾讯云桶
    private JTextField serverId,activityId,sqlAddress,sqlPort,userName,password,bucket;
    //                 日志     数据库文件 生成文件 路径
    private JTextField logPath,sqlPath,createPath;
    //按钮
    private JButton logPathBtn,sqlPathBtn,createPathBtn, createBtn,clearBtn,testBtn;

    public ConfigCreate(Container container,MainFrame mainFrame){

        super(container,mainFrame);

        initRemark("<html><font color='white'>1.一键生成配置文件<br/>" +
                "2.输入log路径,服务器id,活动服id<br/>" +
                "3.所有路径不能有中文" +
                "</font></html>",70);

        JLabel serverTitle = new JLabel();
        serverTitle.setBounds(20,110,120,30);
        serverTitle.setFont(MainFrame.font18);
        serverTitle.setForeground(new Color(0, 0, 0));
        serverTitle.setText("<html><font color='white'>" +
                "服务器ID:" +
                "</font></html>");
        container.add(serverTitle);

        //10是限制长度
        serverId = new JTextField(10);
        serverId.setBounds(150, 110, 120, 30);
        serverId.setFont(MainFrame.font18);
        container.add(serverId);

        JLabel activityTitle = new JLabel();
        activityTitle.setBounds(300,110,120,30);
        activityTitle.setFont(MainFrame.font18);
        activityTitle.setForeground(new Color(0, 0, 0));
        activityTitle.setText("<html><font color='white'>" +
                "活动服ID:" +
                "</font></html>");
        container.add(activityTitle);

        activityId = new JTextField(10);
        activityId.setBounds(450, 110, 120, 30);
        activityId.setFont(MainFrame.font18);
        container.add(activityId);

        JLabel sqlAddressTitle = new JLabel();
        sqlAddressTitle.setBounds(20,150,120,30);
        sqlAddressTitle.setFont(MainFrame.font18);
        sqlAddressTitle.setForeground(new Color(0, 0, 0));
        sqlAddressTitle.setText("<html><font color='white'>" +
                "数据库地址:" +
                "</font></html>");
        container.add(sqlAddressTitle);

        sqlAddress = new JTextField(20);
        sqlAddress.setBounds(150,150,120,30);
        sqlAddress.setFont(MainFrame.font18);
        container.add(sqlAddress);

        JLabel sqlPortTitle = new JLabel();
        sqlPortTitle.setBounds(300,150,120,30);
        sqlPortTitle.setFont(MainFrame.font18);
        sqlPortTitle.setForeground(new Color(0, 0, 0));
        sqlPortTitle.setText("<html><font color='white'>" +
                "数据库端口:" +
                "</font></html>");
        container.add(sqlPortTitle);

        sqlPort = new JTextField(10);
        sqlPort.setBounds(450,150,120,30);
        sqlPort.setFont(MainFrame.font18);
        container.add(sqlPort);

        JLabel userNameTitle = new JLabel();
        userNameTitle.setBounds(20,190,120,30);
        userNameTitle.setFont(MainFrame.font18);
        userNameTitle.setForeground(new Color(0, 0, 0));
        userNameTitle.setText("<html><font color='white'>" +
                "用户名:" +
                "</font></html>");
        container.add(userNameTitle);

        userName = new JTextField(10);
        userName.setBounds(150,190,120,30);
        userName.setFont(MainFrame.font18);
        container.add(userName);

        JLabel passwordTitle = new JLabel();
        passwordTitle.setBounds(300,190,120,30);
        passwordTitle.setFont(MainFrame.font18);
        passwordTitle.setForeground(new Color(0, 0, 0));
        passwordTitle.setText("<html><font color='white'>" +
                "密码:" +
                "</font></html>");
        container.add(passwordTitle);

        password = new JTextField(40);
        password.setBounds(450,190,120,30);
        password.setFont(MainFrame.font18);
        container.add(password);

        JLabel bucketTitle = new JLabel();
        bucketTitle.setBounds(20,230,120,30);
        bucketTitle.setFont(MainFrame.font18);
        bucketTitle.setForeground(new Color(0, 0, 0));
        bucketTitle.setText("<html><font color='white'>" +
                "桶:" +
                "</font></html>");
        container.add(bucketTitle);

        bucket = new JTextField(40);
        bucket.setBounds(150,230,120,30);
        bucket.setFont(MainFrame.font18);
        container.add(bucket);

        logPathBtn = new JButton("日志路径");
        logPathBtn.setBounds(20,280,120,30);
        logPathBtn.setFont(MainFrame.font18);
        container.add(logPathBtn);

        logPath = new JTextField(50);
        logPath.setBounds(170,280,400,30);
        logPath.setFont(MainFrame.font18);
        logPath.setEditable(false);
        container.add(logPath);

        sqlPathBtn = new JButton("sql路径");
        sqlPathBtn.setBounds(20,320,120,30);
        sqlPathBtn.setFont(MainFrame.font18);
        container.add(sqlPathBtn);

        sqlPath = new JTextField(50);
        sqlPath.setBounds(170,320,400,30);
        sqlPath.setFont(MainFrame.font18);
        sqlPath.setEditable(false);
        container.add(sqlPath);

        createPathBtn = new JButton("生成路径");
        createPathBtn.setBounds(20,360,120,30);
        createPathBtn.setFont(MainFrame.font18);
        container.add(createPathBtn);

        createPath = new JTextField(50);
        createPath.setBounds(170,360,400,30);
        createPath.setFont(MainFrame.font18);
        createPath.setEditable(false);
        container.add(createPath);

        contentText = new JTextArea();
        contentText.setFont(new Font("宋体", 0, 16));
        contentText.setEditable(false);
        contentText.setForeground(Color.WHITE);
        contentText.setBorder(null);
        contentText.setBackground(new Color(19,71,100));

        JScrollPane infoJSP = new JScrollPane(contentText);
        infoJSP.setBounds(20, 420, 600, 320);
        container.add(infoJSP);


        testBtn = new JButton("验证参数");
        testBtn.setBounds(40,780,130,30);
        testBtn.setFont(MainFrame.font18);
        container.add(testBtn);

        createBtn = new JButton("一键生成");
        createBtn.setBounds(260,780,130,30);
        createBtn.setFont(MainFrame.font18);
        container.add(createBtn);

        clearBtn = new JButton("清除日志");
        clearBtn.setBounds(480,780,130,30);
        clearBtn.setFont(MainFrame.font18);
        container.add(clearBtn);

        logPathBtn.addActionListener((e) -> {
            actionListener(e);
        });

        sqlPathBtn.addActionListener((e)->{
            actionListener(e);
        });

        createPathBtn.addActionListener((e)->{
            actionListener(e);
        });

        testBtn.addActionListener((e)->{
            actionListener(e);
        });

        createBtn.addActionListener((e) -> {
            actionListener(e);
        });

        clearBtn.addActionListener((e)->{
            actionListener(e);
        });

        initEnd("配置文件生成工具");
    }

    private void actionListener(ActionEvent event){
        startActionListener();
        Object source = event.getSource();
        String result = "";
        if (source == logPathBtn || source == createPathBtn){
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (source == logPathBtn){
                jfc.setCurrentDirectory(new File(""));
                jfc.showDialog(new JLabel(),"选择日志输出路径");
            }
            else if (source == createPathBtn){
                jfc.setCurrentDirectory(new File("C:"));
                jfc.showDialog(new Label(),"选择生成路径");
            }
            File file = jfc.getSelectedFile();
            if (file != null){
                if (source == logPathBtn)
                    logPath.setText(file.getAbsolutePath());
                else if(source == createPathBtn)
                    createPath.setText(file.getAbsolutePath());
            }
        }
        else if(source == sqlPathBtn){
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);

            jfc.setCurrentDirectory(new File("D:"));
            jfc.showDialog(new Label(),"选择sql文件");
            File file = jfc.getSelectedFile();
            sqlPath.setText(file.getAbsolutePath());
        }
        else if(source == testBtn){
            if (!testSource()){
                println("参数有错误！！！");
            }
        }
        else if(source == createBtn){
            println("开始启动一键生成配置文件");
            if (!testSource()){
                println("配置错误 ,配置文件生成终止 ...");
            }else{
                if (createConfig()){
                    println("配置文件生成完成");
                    println("OK");
                }else{
                    println("配置文件生成错误");
                    println("请稍后再试!");
                    JOptionPane.showMessageDialog(mainFrame,"配置文件生成错误");
                }
            }
        }else if(source == clearBtn){
            contentText.setText("");
        }
        endActionListener();
    }

    private boolean testSource(){
        println("开始验证参数 ...");
        String sId = serverId.getText();
        if (Utils.isNullOrEmpty(sId)){
            println("请填写服务器ID");
            JOptionPane.showMessageDialog(mainFrame,"请填写服务器ID");
            return false;
        }
        try {
            Integer.valueOf(sId);
        }catch (IllegalFormatException e){
            e.printStackTrace();
            println("服务器ID必须是数字");
            JOptionPane.showMessageDialog(mainFrame,"服务器ID必须是数字");
            return false;
        }

        String aId = activityId.getText();
        if (Utils.isNullOrEmpty(aId)){
            println("请填写活动服ID");
            JOptionPane.showMessageDialog(mainFrame,"请填写活动服ID");
            return false;
        }
        try{
            Integer.valueOf(aId);
        }catch (Exception e){
            println("活动服ID必须是数字");
            JOptionPane.showMessageDialog(mainFrame,"活动服ID必须是数字");
            return false;
        }
        String ip = sqlAddress.getText();
        if (Utils.isNullOrEmpty(ip)){
            println("数据库地址不能为空");
            JOptionPane.showMessageDialog(mainFrame,"数据库地址不能为空");
            return false;
        }
        if (!CCUtils.isBoolIp(ip)){
            println("数据库地址格式不正确");
            JOptionPane.showMessageDialog(mainFrame,"数据库地址格式不正确,e: 127.0.0.1");
            return false;
        }
        String port = sqlPort.getText();
        if (Utils.isNullOrEmpty(port)){
            println("数据库端口不能为空");
            JOptionPane.showMessageDialog(mainFrame,"数据库端口不能为空");
            return false;
        }
        if (!CCUtils.isPort(port)){
            println("数据库端口格式不正确");
            JOptionPane.showMessageDialog(mainFrame, "数据库端口格式不正确 1-65535");
            return false;
        }
        if (Utils.isNullOrEmpty(userName.getText())){
            println("用户名不能为空");
            JOptionPane.showMessageDialog(mainFrame, "用户名不能为空");
            return false;
        }
        if (Utils.isNullOrEmpty(password.getText())){
            println("密码不能为空");
            JOptionPane.showMessageDialog(mainFrame, "密码不能为空");
            return false;
        }
        if (Utils.isNullOrEmpty(bucket.getText())){
            println("桶不能为空");
            JOptionPane.showMessageDialog(mainFrame, "桶不能为空");
            return false;
        }
        if (Utils.isNullOrEmpty(logPath.getText())){
            println("日志路径不能为空");
            JOptionPane.showMessageDialog(mainFrame, "请选择日志路径");
            return false;
        }
        File log = new File(logPath.getText());
        if (!log.exists() || !log.isDirectory()){
            println(logPath.getText() + "日志路径不存在");
            JOptionPane.showMessageDialog(mainFrame, "日志路径不存在");
            return false;
        }
        if (Utils.isNullOrEmpty(sqlPath.getText())){
            println("sql路径不能为空");
            JOptionPane.showMessageDialog(mainFrame, "请选择sql路径");
            return false;
        }
        File sql = new File(sqlPath.getText());
        if (!sql.exists() || !sql.isFile()){
            println(sqlPath.getText() + "sql路径不存在");
            JOptionPane.showMessageDialog(mainFrame, "sql路径不存在");
            return false;
        }
        if (Utils.isNullOrEmpty(createPath.getText())){
            println("配置生成路径不能为空");
            JOptionPane.showMessageDialog(mainFrame, "请选择配置生成路径");
            return false;
        }
        File create = new File(createPath.getText());
        if (!create.exists() || !create.isDirectory()){
            println(createPath.getText() + "配置生成路径不存在");
            JOptionPane.showMessageDialog(mainFrame, "配置生成路径不存在");
            return false;
        }
        println("验证参数完成。");
        return true;
    }


    private boolean createConfig(){
        String sId = serverId.getText();
        String aId = activityId.getText();
        String ip = sqlAddress.getText();
        String port = sqlPort.getText();
        String name = userName.getText();
        String pass = password.getText();
        String bu = bucket.getText();
        String log = logPath.getText();
        String sql = sqlPath.getText();
        String create = createPath.getText();
        return CCUtils.createConfig(sId,aId,ip,port,name,pass,bu,log,sql,create,this);
    }


}
